package com.xmzs.midjourney.handler.listener;

import com.neovisionaries.ws.client.ProxySettings;
import com.neovisionaries.ws.client.WebSocketFactory;
import com.xmzs.common.core.config.ProxyConfig;
import com.xmzs.midjourney.domain.MjServer;
import com.xmzs.midjourney.handler.MjServerHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author hncboy
 * @date 2023/5/18
 * Discord 启动类
 */
@Slf4j
@Component("discordStarter")
public class DiscordStarter implements InitializingBean {

    @Resource
    private ProxyConfig proxyConfig;

    @Resource
    private DiscordMessageListener discordMessageListener;
    @Resource
    private MjServerHandler mjServerHandler;

    @Override
    public void afterPropertiesSet() {
        log.info("DiscordStarter 开始启动");
        // 因为 Discord 配置需要初始化，所以改了 BotToken 改了要重启
        List<MjServer> mjServerList = mjServerHandler.getAllMjServer();
        for(MjServer mjServer:mjServerList){
            // TODO 有时间可以尝试改成 userToken 试下效果
            DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createDefault(mjServer.getBotToken(), GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT);
            builder.addEventListeners(this.discordMessageListener);
            log.info("Midjourney 开始启动");

            // 判断是否需要代理，下面和代理相关
            if (proxyConfig.getEnabled()) {
                // 解决报错：java.net.SocketTimeoutException: Connect timed out
                OkHttpClient.Builder okhttpbuilder = new OkHttpClient.Builder();
                okhttpbuilder.proxy(proxyConfig.getProxy());
                builder.setHttpClientBuilder(okhttpbuilder);

                // 解决报错：com.neovisionaries.ws.client.WebSocketException: Failed to connect to 'gateway.discord.gg:443': Connect timed out
                WebSocketFactory webSocketFactory = new WebSocketFactory();
                ProxySettings proxySettings = webSocketFactory.getProxySettings();
                proxySettings.setHost(proxyConfig.getHttpHost());
                proxySettings.setPort(proxyConfig.getHttpPort());
                builder.setWebsocketFactory(webSocketFactory);
            }
            try {
                builder.build();
            }catch (Exception e){
                log.error(e.toString());
            }
        }


    }
}
